/*************************************************************************
	> File Name: B3642.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2024-12-25 10:25:54
 ************************************************************************/
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 1000000

typedef struct Node {
    int data;
    int lchild, rchild;
} Node;

void pre_order(vector<Node> &tree, int ind) {
    if (ind == 0) return ;
    cout << tree[ind].data << " ";
    pre_order(tree, tree[ind].lchild);
    pre_order(tree, tree[ind].rchild);
}

void in_order(vector<Node> &tree, int ind) {
    if (ind == 0) return ;
    in_order(tree, tree[ind].lchild);
    cout << tree[ind].data << " ";
    in_order(tree, tree[ind].rchild);
}

void post_order(vector<Node> &tree, int ind) {
    if (ind == 0) return ;
    post_order(tree, tree[ind].lchild);
    post_order(tree, tree[ind].rchild);
    cout << tree[ind].data << " ";
}

int main() {
    int n;
    cin >> n;
    vector<Node> tree(n + 1);
    for (int i = 1; i <= n; i++) {
        int a, b;
        cin >> a >> b;
        tree[i].data = i;
        tree[i].lchild = a;
        tree[i].rchild = b;
    }
    pre_order(tree, 1);
    cout << endl;
    in_order(tree, 1);
    cout << endl;
    post_order(tree, 1);
    cout << endl;
    return 0;
}
